﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DAL;
using System.Data;

namespace BLL
{
  public class ArticleBLL
  {
    //Get all

    public List<Article> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Article> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.isEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Article> GetByList(object list)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Article> listArticle = new List<Article>();
        List<Slide> listSlide = ((List<Slide>)list);
        foreach (var item in listSlide)
        {
          Article obj = GetById(item.Id);
          listArticle.Add(obj);
        }
        return listArticle;
      }      
    }

    //Get by Random 
    

    //Get by category

    public List<Article> GetByCategory(int idCategory)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == idCategory).OrderByDescending(p => p.CreateDate).ToList();
      }
    }
    public List<Article> GetByCategorys(int idCategory, int idNow)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Articles.Where(p => p.CategoryId == idCategory && p.Id != idNow).OrderByDescending(p => p.CreateDate).ToList();
        }
    }

    public List<Article> GetByCategory(int cateId, bool isEnable, int page, int size, out int countItem)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            int skip = (page - 1) * size;
            List<Article> list = data.Articles.Where(p => p.CategoryId == cateId && p.isEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
            countItem = list.Count;
            return list.Skip(skip).Take(size).ToList();
        }
    }

    public List<Article> GetByCategory(int idCategory, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == idCategory).OrderByDescending(p => p.CreateDate).Skip(0).Take(top).ToList();
      }
    }
    public List<Article> GetByCategory(int idCategory, int top, int otherId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Articles.Where(p => p.CategoryId == idCategory && p.Id != otherId).OrderByDescending(p => p.CreateDate).Skip(0).Take(top).ToList();
        }
    }
    public List<Article> GetByCategory(int idCategory, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == idCategory && p.isEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Article> GetByCategoryDescending(int idCategory, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == idCategory && p.isEnable == isEnable).OrderBy(p => p.CreateDate).ToList();
      }
    }

    //Get by page

    public List<Article> GetByPage(List<Article> list, int page, int size)
    {
      page--;
      int skip = page * size;
      return list.Skip(skip).Take(size).ToList();
    }

    //Get by Tag

    public List<Article> GetByTag(string strTag)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.Tag.Contains(strTag)).OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Article> GetByTag(string strTag, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.Tag.Contains(strTag) && p.isEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Article> GetByView(int categoryId, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == categoryId && p.isEnable == true).OrderByDescending(p => p.View).Take(top).ToList();
      }
    }

    public List<Article> GetByViewNotCateId(int categoryId,int top)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Articles.Where(p => p.CategoryId != categoryId && p.isEnable == true).OrderByDescending(p => p.View).Take(top).ToList();
        }
    }

    //Get by id

    public Article GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.Id == id).FirstOrDefault();
      }
    }
    public List<Article> GetByCateParentId(int cateId, bool isEnable, int size)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            List<Category> list = data.Categories.Where(p => p.Parent == cateId && p.isEnable == true).ToList();
            List<Article> listArticle = new List<Article>();
            foreach (var item in list)
            {
                List<Article> products = GetByCategory(item.Id);
                listArticle.AddRange(products);
            }
            return listArticle.Take(size).OrderBy(p => p.CreateDate).ToList();
        }
    }
    public Article GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article obj = data.Articles.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article obj = data.Articles.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }

    //Add View

    public void AddView(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article obj = data.Articles.Where(p => p.Id == id).FirstOrDefault();
        obj.View = obj.View + 1;

        data.SubmitChanges();
      }
    }

   //Add

    public void Add(Article obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Articles.InsertOnSubmit(obj);

        data.SubmitChanges();
      }
    }

   //Update

    public void Update(Article obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article article = data.Articles.Where(p => p.Id == obj.Id).FirstOrDefault();

        article.CategoryId = obj.CategoryId;
        article.VideoId = obj.VideoId;
        article.GalleryParentId = obj.GalleryParentId;
        article.Title = obj.Title;
        article.Title_En = obj.Title_En;
        article.Content = obj.Content;
        article.Content_En = obj.Content_En;
        article.CreateDate = obj.CreateDate;
        article.Description = obj.Description;
        article.Description_En = obj.Description_En;
        article.Image = obj.Image;
        article.isEnable = obj.isEnable;
        article.Tag = obj.Tag;
        article.View = obj.View;

        data.SubmitChanges();
      }
    }

    //Delete

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article obj = data.Articles.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
            ArticleHotBLL db = new ArticleHotBLL();
            db.Delete(obj.Id);

          data.Articles.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }

    public void DeleteByCate(int idCategory)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Article> obj = data.Articles.Where(p => p.CategoryId == idCategory).ToList();
        if (obj != null)
        {
          data.Articles.DeleteAllOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
  }
}
